// 寻物与出物
var express = require('express');
var router = express.Router();
var db = require('../model/index');

/* GET  */
router.get('/barterList', function (req, res) {
    //   res.send('respond with a resource');
    var sql = `SELECT * FROM barterlist`
    db(sql, null, function (err, data) {
        if (!err) {
            //    console.log(123);
            if (data.length > 0) {
                res.send({
                    code: 200,
                    message: "success",
                    data: data,
                })

            } else {
                res.send({
                    code: 500,
                    message: "fail"
                })
            }

        } else {
            res.send({
                code: 501,
                message: "dbfail"
            })
        }
    })

});
router.get('/barterImg', function (req, res) {
    //   res.send('respond with a resource');
    var sql = `SELECT * FROM barterImg where barterId = ?`
    db(sql, [req.query.barterId], function (err, data) {
        if (!err) {
            //    console.log(123);
            if (data.length > 0) {
                res.send({
                    code: 200,
                    message: "success",
                    data: data,
                })

            } else {
                res.send({
                    code: 500,
                    message: "fail"
                })
            }

        } else {
            res.send({
                code: 501,
                message: "dbfail"
            })
        }
    })

});
// 寻物留言
router.get('/updateBarter', function (req, res) {
    //   res.send('respond with a resource');
    // if(req.query.leaveMessageAudit == 1){
    //     var sql = `UPDATE bartermessage SET leaveMessage=? WHERE barterId = ? and userId = ?`
    // }else{

    // // var sql = `INSERT into bartermessage (barterId,userId,leaveMessage) VALUES (1,2,'hellp')`
    // }
    var sql = `INSERT into bartermessage (leaveMessage,barterId,userId,leaveUser) VALUES (?,?,?,?);UPDATE barterlist SET leaveMessageAudit=1 WHERE barterId = ?`
    db(sql, [req.query.leaveMessage, req.query.barterId, req.query.userId, req.query.leaveUser, req.query.barterId], function (err, data) {
        //    db(sql,null,function(err,data){

        if (!err) {
            //    console.log(123);
            res.send({
                code: 200,
                message: "success",
                data: data,
            })
        } else {
            res.send({
                code: 501,
                message: "dbfail"
            })
        }
    })

});
// 寻物举报
router.get('/jubaoBarter', function (req, res) {
    var sql = `INSERT into barterjubao (jubao,barterId,userId) VALUES (?,?,?);UPDATE barterlist SET barterAudit=2 WHERE barterId = ?`
    db(sql, [req.query.jubao, req.query.barterId, req.query.userId, req.query.barterId], function (err, data) {
        if (!err) {
            //    console.log(123);
            res.send({
                code: 200,
                message: "success",
                data: data,
            })
        } else {
            res.send({
                code: 501,
                message: "dbfail"
            })
        }
    });

});
// 寻物添加
router.post('/addBarter', function (req, res) {
    console.log(req.body);
    var sql = `INSERT into barterlist (InfoPublisher,barterAudit,
            barterContent,
            barterDesc,
            barterImg,
            barterTagType,
            barterTags,
            barterTitle,
            barterType,
            barterValidDate,
            leaveMessageAudit) VALUES (?)
            `
    //   select @phoen:=?
    // select @phoen:=barterId from barterlist WHERE InfoPublisher=? and barterAudit=? and 
    // barterContent=? and 
    // barterDesc=? and 
    // barterImg=? and 
    // barterTagType=? and 
    // barterTags=? and 
    // barterTitle=? and 
    // barterType=? and 
    // barterValidDate=? and 
    // leaveMessageAudit=?;
    // INSERT INTO barterimg (barterId) VALUES( @phoen);  

    let params = [req.body.InfoPublisher, req.body.barterAudit,
    req.body.barterContent,
    req.body.barterDesc,
    req.body.barterImg,
    req.body.barterTagType,
    req.body.barterTags,
    req.body.barterTitle,
    req.body.barterType,
    req.body.barterValidDate,
    req.body.leaveMessageAudit]


    // let params = req.body.barterImg;
    // console.log(params,"tesr")
    // var form =new formidable.IncomingForm();
    // console.log(fields,"tesr");
    // form.parse(req,(err,fields,files)=>{
    // let id = fields.barterImg;
    // console.log(id,"id");
    // });

    db(sql, [params], function (err, data) {
        if (!err) {
            //    console.log(123);
            res.send({
                code: 200,
                message: "success",
                data: data,
            })
        } else {
            res.send({
                code: 501,
                message: "dbfail"
            })
        }
    });

});
// 寻物图片批量
router.post('/addBarterImgs', function (req, res) {
    let arr = req.body.barterImgs; // 前端提交过来的数据
    console.log(arr, 11);
    let sql = `INSERT INTO barterimg (barterId,barterImg) VALUES(?,?)`    // 插入语句
    for (let i = 0; i < arr.length; i++) {
        db(sql, [req.body.barterId, arr[i]], function (err, data) {
            if (err) {
                console.log(err)
                if (i == arr.length - 1) {
                    res.send({
                        code: 500,
                        msg: "添加失败!",
                    });
                }
            } else {
                console.log(data);
                if (i == arr.length - 1) {
                    res.send({
                        msg: "添加成功!",
                        code: 200
                    });
                }
            }
        });
    }
});
// 寻物搜索
router.get('/searchTitle', function (req, res) {
    console.log(req.query.searchTitle);
    var sql = `select * from barterlist where barterTitle  REGEXP ?`
    db(sql, [req.query.searchTitle], function (err, data) {
        if (!err) {
            res.send({
                code: 200,
                message: "success",
                data: data,
            })
        } else {
            res.send({
                code: 501,
                message: "dbfail",
            })
        }
    });

});
module.exports = router;